package com.example.red_tea_house.moudle.LoginAndRegister.controller;

import android.content.Context;

import com.example.red_tea_house.MyApplication;
import com.example.red_tea_house.annotation.UrlType;
import com.example.red_tea_house.entity.LoginBean;
import com.example.red_tea_house.moudle.LoginAndRegister.model.LegalData;
import com.example.red_tea_house.util.ToastUtil;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;


import okhttp3.Call;



public abstract class GetVertifyController {
    private final String sendVertifyCodeUrl="sendvertificationcode";
    private final String judgeVertifyCodeUrl="judgevertifycode";

    private String vertifiCode;
    private boolean returnJudge=false;

    private LoginBean loginBean=new LoginBean();
    LegalData legalData=new LegalData();

    Context context= MyApplication.getContext();
   public String getIdentifycode(String phone) {
        loginBean.setPhone(phone);
        loginBean.setPassword("111111");//填充一个数据防止因为没有密码而报错
        if(legalData.isLegalData(loginBean)) {
            sendVertifyCodeHttp(phone);
        }else {
            return null;
        }
        return vertifiCode;
    }
    /*
    * TODO:发送验证码时，应该有如果一分钟内发过，就不能再次点击？
    * */
    private void sendVertifyCodeHttp(String phone){
        OkHttpUtils
                .get()
                .url(UrlType.REMOTESERVER_URL +sendVertifyCodeUrl+"?phone="+phone)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        ToastUtil.showToast(context,"发送失败");
                        //ToastUtil.showToast(context,e.getMessage());
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Gson gson = new Gson();
                        vertifiCode = gson.fromJson(response,String.class);
                        ToastUtil.showToast(context, "成功发送验证码,三分钟内有效");
                        ToastUtil.showToast(context,vertifiCode);
                    }
                });
    }


    public void sendJudgeRequest(String inputVertifyCode, String phone){
        OkHttpUtils
                .get()
                .url(UrlType.REMOTESERVER_URL +judgeVertifyCodeUrl)
                .addParams("vertifycode",inputVertifyCode)
                .addParams("phone",phone)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        ToastUtil.showToast(context,e.getMessage());
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Gson gson=new Gson();
                        returnJudge = gson.fromJson(response,boolean.class);
                        vertifySucess();
                    }
                });
    }

    protected abstract void vertifySucess();
    /*
    * TODO:为验证失败也添加一个虚函数
    * */
}


